package com.tjb.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tjb.reggie.common.BaseContext;
import com.tjb.reggie.common.R;
import com.tjb.reggie.entity.ShoppingCart;
import com.tjb.reggie.service.ShoppingCarService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author tjb
 * @version 1.0
 * @date 2022/8/26 16:30
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCarController {
    @Resource
    private ShoppingCarService shoppingCarService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户的id，指定当前是哪个用户的购物车数据
        Long userId = BaseContext.getCurrentUserId();
        shoppingCart.setUserId(userId);

        //查询当前的菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        //构造器
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId!=null, ShoppingCart::getUserId, userId);
        //wrapper.like(ShoppingCart::getCreateTime, LocalDateTime.now());
       if (dishId != null){
           wrapper.eq(ShoppingCart::getDishId, dishId);
       }
       if (setmealId != null){
           wrapper.eq(ShoppingCart::getSetmealId, setmealId);
       }
       //查询当前的菜品或者曹参是否在购物车中
        ShoppingCart one = shoppingCarService.getOne(wrapper);
        if (one != null){
        //如果存在，就把存在的那一份数量加1，不存在就添加到购物车
            one.setNumber(one.getNumber() + 1);
            shoppingCarService.updateById(one);
        }
        //不存在,添加到购物侧,数量默认就是一
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCarService.save(shoppingCart);
        one = shoppingCart;
        return R.success(one);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        //根据用户的id，查询用户的购物车
        Long userId = BaseContext.getCurrentUserId();
        //构造器
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //根据用户的id查询
        wrapper.eq(ShoppingCart::getUserId, userId);
        //和现在时间差不多的
        //wrapper.like(ShoppingCart::getCreateTime, LocalDateTime.now());
        //根据创建时间倒序排列
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> cartLists = shoppingCarService.list(wrapper);
        return R.success(cartLists);
    }
    @DeleteMapping("clean")
    public R<String> deleteByUserId(){
        Long userId = BaseContext.getCurrentUserId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        //wrapper.like(ShoppingCart::getCreateTime, LocalDateTime.now());
        shoppingCarService.remove(wrapper);
        return R.success("购物车清空成功");
    }
    /**
     * 菜品清除
     */
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        Long userId = BaseContext.getCurrentUserId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        if (shoppingCart.getDishId() == null){
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        if (shoppingCart.getSetmealId() == null){
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        shoppingCarService.remove(wrapper);
        return R.success("成功");
    }
}
